<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">

</head>
<body>

<!-- 更新月销售额弹出层 -->
<div id="update-user-layer" style="display: none; padding: 20px">
    <form id="update-user-form" class="layui-form layui-form-pane" lay-filter="update-user-form">
        <div class="layui-form-item">
            <label class="layui-form-label">月份</label>
            <div class="layui-input-block">
                <input type="text" name="date" id="date" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">月销售额</label>
            <div class="layui-input-block">
                <input type="text" name="sales_amount" id="sales_amount" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="update-user-form-submit">提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

<div class="layuimini-container">
    <div class="layuimini-main">

        <div class="demoTable">
            月份：
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="date" id="demoReload1" placeholder="yyyy-MM">
                </div>
            </div>
            <button class="layui-btn" data-type="reload">搜索</button>
        </div>

<!--        <div class="layui-col-md12">-->
            <div class="layui-card">
                <div class="layui-card-body">
                    <div id="echarts-records" style="width: 100%;min-height:500px"></div>
                </div>
            </div>
<!--        </div>-->

        <script type="text/html" id="toolbarDemo">
<!--            <div class="layui-btn-container">-->
<!--                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>-->
<!--            </div>-->
        </script>

        <table class="layui-hide" id="dailysales" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
    </div>

</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use('laydate', function() {
        var laydate = layui.laydate;

        //年月选择器
        laydate.render({
            elem: '#demoReload1'
            ,type: 'month'
        });
    });

    layui.use('laydate', function() {
        var laydate = layui.laydate;

        laydate.render({
            elem: '#date'
            ,type:'month'
        });
    });

    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            monthlysales = layui.table;

        monthlysales.render({
            elem: '#dailysales',
            url:'/getAllmonthlysales',
            toolbar: '#toolbarDemo',
            title:'月销售额',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field: 'date', width: 650, title: '月份',sort:true},
                {field: 'sales_amount', minWidth: 600, title: '销售额',sort:true},
                // {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            id: 'testReload',
        });
        var $ = layui.$, active = {
            reload: function() {

                var demoReload1 = $('#demoReload1');

                monthlysales.reload('testReload', {
                    url: '/getByMonthlyDate',
                    where: {
                        year_month: demoReload1.val()
                    }
                }, 'data');

            }
        };

        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        /**
         * toolbar监听事件
         */
        monthlysales.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加月销售额',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '../page/table/addmonthlysales.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        //监听表格复选框选择
        monthlysales.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        monthlysales.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                // 每次显示更新销售额的表单前自动为表单填写该行的数据
                form.val('update-user-form', {
                    "date":data.date,
                    "sales_amount":data.sales_amount,
                });
                // 显示更新销售额表单的弹出层
                layer.open({
                    type: 1,
                    title: '编辑月销售额',
                    skin: 'layui-layer-molv',
                    area: ['500px'],
                    content: $('#update-user-layer')
                });
                // 更新月销售额表单提交
                form.on('submit(update-user-form-submit)', function(data) {
                    var param=data.field;//定义临时变量获取表单提交过来的数据，非json格式
                    console.log(param);//测试是否获取到表单数据，调试模式下在页面控制台查看
                    // ajax方式更新商品
                    $.ajax({
                        url: "/modifyMonthlysales",
                        type:"PUT",
                        data: JSON.stringify(data.field),
                        contentType: 'application/json;charset=utf-8',
                        dataType: 'json',
                        success: function(data) {
                            if (data.status == 1) {
                                layer.close(layer.index);
                                layer.msg('更新成功');
                                monthlysales.reload('testReload');
                            } else {
                                layer.msg('更新失败');
                            }
                        },
                        error: function() {
                            console.log("ajax error");
                        }
                    });
                    // 阻止表单跳转
                    return false;
                });

            } else if (obj.event === 'delete') {
                layer.confirm('确定删除该月销售额吗', function (index) {
                    $.ajax({
                        url:"/deleteByMonthlyDate",
                        data:{"year_month":data.date},
                        success:function (data) {
                            if(data.status==1){
                                layer.msg("删除成功");
                                obj.del();
                            }else {
                                layer.msg("删除失败");
                            }

                        }
                    })
                    layer.close(index);
                });
            }
        });

    });
    layui.use(['layer','echarts'], function () {
        var $ = layui.jquery,
            echarts = layui.echarts;
        var echartOrder = echarts.init(document.getElementById('echarts-records'), 'walden');
        var series1 = [];
        var series2 = [];
        $.ajax({
            url: '/getAllmonthlysales',
            type: "GET",
            content: "application/json",
            dataType: 'json',
            data: {},
            success: function (res) {
                $.each(res.data, function (index, item) {
                    series1.push(item.date);
                });
                $.each(res.data, function (index, item) {
                    series2.push(item.sales_amount);
                });
                var data = res.data;
                console.log(data);
                var echartOrderZhe = {
                    title: {
                        text: '月销售额统计'
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    toolbox: {
                        feature: {
                            saveAsImage: {}
                        }
                    },
                    xAxis: {
                        data: series1
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [{
                        name: '金额',
                        type: 'line',
                        data: series2,
                    }]
                };
                echartOrder.setOption(echartOrderZhe);
            }
        })
    });
</script>

</body>
</html>